home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / listings / v_09_08 / 9n08064a < prev    next >
Text File  |  1991-06-19  |  12KB  |  497 lines

  1.  
  2.  
  3. Listing 1 - hist.c - Histogram Operations
  4.  
  5.  
  6.  
  7.  
  8.        /*****************************************************
  9.        *
  10.        *       file d:\cips\hist.c
  11.        *
  12.        *       Functions: This file contains
  13.        *           calculate_histogram
  14.        *           calculate_histogram
  15.        *           zero_histogram
  16.        *           perform_histogram_equalization
  17.        *           show_histogram
  18.        *           print_histogram
  19.        *           smooth_histogram
  20.        *
  21.        *
  22.        *       Purpose: These functions calculate and display the
  23.        *          histogram of an input image array.
  24.        *
  25.        *       Modifications:
  26.        *           July 86 - ported to IBM-PC
  27.        *           August 1990 - modified for use in the
  28.        *               C Image Processing System
  29.        *
  30.        *******************************************************/
  31.  
  32. #include "d:\cips\cips.h"
  33.  
  34.  
  35. #define PRINT_WIDTH  80
  36. #define FORMFEED     '\014'
  37.  
  38.  
  39.  
  40.  
  41.         /*****************************************
  42.         *
  43.         *   zero_histogram(...
  44.         *
  45.         ******************************************/
  46.  
  47. zero_histogram(histogram)
  48.    unsigned long histogram[];
  49. {
  50.    int i;
  51.    for(i=0; i<256; i++)
  52.       histogram[i] = 0;
  53. }  /* ends zero_histogram */
  54.  
  55.  
  56.  
  57.  
  58.  
  59.  
  60.  
  61.         /*****************************************
  62.         *
  63.         *   calcualte_histogram(...
  64.         *
  65.         ******************************************/
  66.  
  67. calculate_histogram(image, histogram)
  68.    short  image[ROWS][COLS];
  69.    unsigned long histogram[];
  70. {
  71.    int i,j,k;
  72.    for(i=0; i<ROWS; i++){
  73.       for(j=0; j<COLS; j++){
  74.          k = image[i][j];
  75.          histogram[k] = histogram[k] + 1;
  76.       }
  77.    }
  78. }  /* ends calculate_histogram */
  79.  
  80.  
  81.  
  82.  
  83.  
  84.  
  85.  
  86.  
  87.  
  88.         /*****************************************
  89.         *
  90.         *    perform_histogram_equalization(...
  91.         *
  92.         ******************************************/
  93.  
  94. perform_histogram_equalization(image, histogram, new_grays, area)
  95.    float new_grays, area;
  96.    short image[ROWS][COLS];
  97.    unsigned long histogram[];
  98. {
  99.    int i,
  100.        j,
  101.        k;
  102.    unsigned long sum,
  103.             sum_of_h[256];
  104.  
  105.    double constant;
  106.  
  107.  
  108.    sum = 0;
  109.    for(i=0; i<256; i++){
  110.       sum         = sum + histogram[i];
  111.       sum_of_h[i] = sum;
  112.    }
  113.  
  114.       /* constant = new # of gray levels div by area */
  115.    constant = new_grays/area;
  116.    for(i=0; i<ROWS; i++){
  117.       for(j=0; j<COLS; j++){
  118.          k           = image[i][j];
  119.          image[i][j] = sum_of_h[k] * constant;
  120.       }
  121.    }
  122. }  /* ends perform_histogram_equalization */
  123.  
  124.  
  125.  
  126.  
  127.  
  128.  
  129.         /*********************************************
  130.         *
  131.         *       show_histogram(histogram)
  132.         *
  133.         *        This function shows the histogram
  134.         *        on the screen as numbers and stars.
  135.         *
  136.         **********************************************/
  137.  
  138.  
  139.  
  140. show_histogram(histogram)
  141.         unsigned long histogram[];
  142. {
  143.         int     count,
  144.                 i,
  145.                 j;
  146.         unsigned long max, scale;
  147.  
  148.  
  149.         max   = 0;
  150.         count = 0;
  151.  
  152.         for(i=0; i<GRAY_LEVELS; i++)
  153.            if(histogram[i] > max)
  154.               max = histogram[i];
  155.  
  156.         if(max > (70 - 12))
  157.            scale = max/(70 - 12);
  158.         else
  159.            scale = 1;
  160.  
  161.         printf("\n max=%ld scale=%ld",max, scale);
  162.  
  163.         printf("\n\ngray    count");
  164.         printf("\nlevel");
  165.  
  166.         for(i=0; i<256; i++){
  167.            if(histogram[i] == 0)
  168.               ++count;
  169.            else 
  170.               count = 0;
  171.  
  172.            if(count < 2){
  173.               printf("\n %4d: %7ld",i,histogram[i]);
  174.               for(j=0; j<((int)(histogram[i]/scale)); j++){
  175.                  printf("*");
  176.               }         /* ends loop over j             */
  177.            }            /* ends if count < 5            */
  178.         }               /* ends loop over i GRAY_LEVELS */
  179. }       /* ends show_histogram */
  180.  
  181.  
  182.  
  183.  
  184.  
  185.  
  186.  
  187.  
  188.  
  189.         /*********************************************
  190.         *
  191.         *        print_histogram(histogram)
  192.         *
  193.         *        This function prints the histogram
  194.         *       input to the function.
  195.         *
  196.         **********************************************/
  197.  
  198.  
  199.  
  200. print_histogram(histogram, name)
  201.         char name[];
  202.         unsigned long histogram[];
  203. {
  204.         char    string[300],
  205.                 output[300];
  206.  
  207.         int     count,
  208.                 i,
  209.                 j,
  210.                 line_counter,
  211.                 print_counter;
  212.         unsigned long scale, max;
  213.  
  214.         FILE *printer;
  215.  
  216.         if( (printer = fopen("prn", "w")) == NULL)
  217.            printf("\nPH> Could not open printer");
  218.         else
  219.            printf("\nPH> The print file is opened");
  220.  
  221.         max           = 0;
  222.         count         = 0;
  223.         print_counter = 0;
  224.  
  225.         for(i=0; i<256; i++)
  226.            if(histogram[i] > max)
  227.               max = histogram[i];
  228.  
  229.         if(max > (PRINT_WIDTH - 12))
  230.            scale = max/(PRINT_WIDTH - 12);
  231.         else
  232.            scale = 1;
  233.  
  234.         printf("\n max=%ld scale=%ld",max, scale);
  235.  
  236.         printf("\nPI> Print header");
  237.         line_counter = 0;
  238.         long_clear_buffer(string);
  239.         sprintf(string, "          This image is -- %s --", name);
  240.         my_fwriteln(printer, string);
  241.         ++line_counter;
  242.  
  243.         long_clear_buffer(string);
  244.         sprintf(string, " ");
  245.         my_fwriteln(printer, string);
  246.         ++line_counter;
  247.  
  248.         long_clear_buffer(string);
  249.         sprintf(string, "          gray    count");
  250.         my_fwriteln(printer, string);
  251.         ++line_counter;
  252.         long_clear_buffer(string);
  253.         sprintf(string, "          level");
  254.         my_fwriteln(printer, string);
  255.         ++line_counter;
  256.  
  257.         for(i=0; i<256; i++){
  258.            if(histogram[i] == 0)
  259.               ++count;
  260.            else 
  261.               count = 0;
  262.            if(count < 2){
  263.               printf(" %4d: %7ld",i,histogram[i]);
  264.               print_counter++;
  265.               if(print_counter >= 6){
  266.                  printf("\n");
  267.                  print_counter = 0;
  268.               }  /* ends if print_counter >= 6 */
  269.  
  270.               long_clear_buffer(string);
  271.               sprintf(string, 
  272.                 "           %3d: %7ld ->",i,histogram[i]);
  273.               my_fwrite(printer, string);
  274.               long_clear_buffer(string);
  275.               sprintf(output, " ");
  276.               for(j=0; j<((int)(histogram[i]/scale)); j++){
  277.                  sprintf(string, "*");
  278.                  append_string(string, output);
  279.               }         /* ends loop over j                */
  280.               my_fwriteln(printer, output);
  281.               ++line_counter;
  282.               if(line_counter >= 55){
  283.                  line_counter = 0;
  284.                  putc(FORMFEED, printer);
  285.               }  /* ends if line_counter >=55  */
  286.  
  287.            }                    /* ends if count < 2 */
  288.         }                 /* ends loop over i */
  289.         putc(FORMFEED, printer);
  290.         fclose(printer);
  291.  
  292. }        /* ends print_histogram */
  293.  
  294.  
  295.  
  296.  
  297.  
  298.  
  299.  
  300.  
  301.         /*********************************************
  302.         *
  303.         *       display_histogram(histogram)
  304.         *
  305.         *       This function shows the histogram
  306.         *       input to the function.
  307.         *
  308.         **********************************************/
  309.  
  310.  
  311.  
  312. display_histogram(histogram, x, y,
  313.                   line_color,data_color)
  314.         int data_color, line_color, x, y;
  315.         unsigned long histogram[];
  316. {
  317.         int     count,
  318.                 i,
  319.                 j,
  320.                 length;
  321.         unsigned long scale, max;
  322.  
  323.         max   = 0;
  324.         count = 0;
  325.  
  326.         for(i=0; i<256; i++)
  327.            if(histogram[i] > max)
  328.               max = histogram[i];
  329.  
  330.         if(max > (300 - 12))
  331.            scale = max/(100 - 12);
  332.         else
  333.            scale = 1;
  334.  
  335.  
  336.    /***************************
  337.    *
  338.    *   clear out an area for
  339.    *   this histogram display
  340.    *
  341.    *************